﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;

namespace CFScript.Tasks
{
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class RegistryDelete : Task
    {
        public RegistryDelete()
        {
            this.name = "<registryDelete>";
            this.parameterNames = new string[] { "key", "name" };
            this.parameterExpandable = new bool[] { true, true };
            this.parameterRequired = new bool[] { true, true };
            this.parameters = new Dictionary<string, Parameter>();

            Debug.Assert(this.parameterNames.Length == this.parameterRequired.Length &&
                         this.parameterRequired.Length == this.parameterRequired.Length);
        }

        public override InstructionResult Execute(CFScript context)
        {
            InstructionResult result = new InstructionResult();
            result.ExecutedInstruction = this;

            string key = this.parameters["key"].GetValue(context);     //Ej: HKEY_LOCAL_MACHINE\cecofar
            string name = this.parameters["name"].GetValue(context);   //Ej: estadoExpedicion

            try
            {
                // TODO: CHIP: Probar esto porque lo que pretendo es borrar una subkey de una key en concreto. Por ejemplo:
                //             "HKEY_LOCAL_MACHINE\cecofar\estadoExpedicion"
                Registry.LocalMachine.DeleteSubKey(key + "\\" + name);
                result.Code = ResultCode.OK;
            }
            catch (Exception ex)
            {
                result.Code = ResultCode.ERROR;
                result.Message = ex.Message;
            }

            result.BreakExecution = result.Code == ResultCode.ERROR && this.options.OnError == OnErrorAction.BREAK;

            return result;
        }
    }
}
